package com.qf.repo.dao.Impl;

import com.qf.repo.bean.Manager;
import com.qf.repo.dao.ManagerDao;
import com.qf.repo.db.DBUtils;
import com.qf.repo.db.PstCallBack;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ManagerDaoImpl implements ManagerDao {
    @Override
    public Manager login(String account, String password) {
        Manager manager = null;
        //1.准备sql语句
        String sql = "select id,account,password,phone,level,remark from manager where account=? and password=?";
        //2.调用DBUtils中的查询方法
        ResultSet resultSet = DBUtils.query(sql, new PstCallBack() {
            @Override
            public void pstSetValue(PreparedStatement pst) {
                try {
                    pst.setString(1,account);
                    pst.setString(2,password);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        try {
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String account1 = resultSet.getString("account");
                String password1 = resultSet.getString("password");
                String phone = resultSet.getString("phone");
                String level = resultSet.getString("level");
                String remark = resultSet.getString("remark");
                manager = new Manager(id,account1,password1,phone,level,remark);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (resultSet!=null){
                    resultSet.close();
                }
                DBUtils.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        return manager;
    }
}
